package com.xingzhe.pengji.security.services.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xingzhe.pengji.security.dao.ResourceIbatisDao;
import com.xingzhe.pengji.security.domain.Resource;
import com.xingzhe.pengji.security.services.ResourceService;

/**
 * @Title ： 三江学院 毕业设计
 * @Description ：
 * @author ：LuFengLiang
 * @Company: IT行者工作室
 * @Copyright: Copyright (c) 2012-2-24
 * @version ： 1.0
 * @param <T>
 */
@Service("resourceService")
public class ResourceServiceImpl<T extends Resource> implements ResourceService<T>
{

    private static final Logger logger = LoggerFactory.getLogger(ResourceServiceImpl.class);
    @Autowired
    private ResourceIbatisDao<T> resourceIbatisDao;

    @Override
    public List<T> queryForPageResourceList(T entity, String sortByColumn, int pageSize, int pageNumber, boolean sortAsc)
    {
        return resourceIbatisDao.queryForPaginatedList(organizeParams(entity), pageNumber, pageSize, sortByColumn,
                sortAsc);
    }

    @Override
    public void saveResource(T entity)
    {
        boolean b = resourceIbatisDao.addResource(entity);
        if ( !b )
        {
            logger.error("新增资源失败");
        }
    }

    @Override
    public void updateResource(T entity)
    {
        boolean b = resourceIbatisDao.updateResource(entity);
        if ( !b )
        {
            logger.error("更新资源失败！");
        }
    }

    @Override
    public void delResource(T entity)
    {
        entity.setDelFlag(-1);
        boolean b = resourceIbatisDao.updateResource(entity);
        if ( !b )
        {
            logger.error("删除资源失败！");
        }
    }

    @Override
    public T getResourceByName(T entity)
    {
        T resource = resourceIbatisDao.getResourceByName(entity);
        if ( null != resource )
        {
            return resource;
        }
        else
        {
            return resource;
        }
    }

    @Override
    public T getResourceById(T entity)
    {
        T resource = resourceIbatisDao.getResourceById(entity);
        if ( null != resource )
        {
            return resource;
        }
        else
        {
            logger.error("该资源不存在");
            return resource;
        }
    }

    @Override
    public int getItemCount(T entity)
    {
        return resourceIbatisDao.getItemCount(organizeParams(entity));
    }

    @SuppressWarnings("unchecked")
    private Map<String, Object> organizeParams(T entity)
    {
        if ( null == entity )
        {
            entity = (T) new Resource();
        }
        if ( null != entity.getName() && ("".equals(entity.getName().trim())) )
        {
            entity.setName(null);
        }
        // 用于存放参数值的容器，用于Ibatis条件查询是取值
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", entity.getId());
        params.put("url", entity.getUrl());
        params.put("name", entity.getName());
        params.put("type", entity.getType());
        params.put("enabled", entity.getEnabled());
        params.put("priority", entity.getPriority());
        params.put("description", entity.getDescription());
        return params;
    }

	@Override
	public List<String> getResourcesByAuthoritieName(String authoritieName) {
		return resourceIbatisDao.getResourcesUrlByAuthoritieName(authoritieName);
	}

}
